package com.itheima.多线程.基础;

public class 线程安全排票代码 {
    private static long count = 100;
    private static Object object=new Object();
    public static void main(String[] args) {
        Thread th1 = getThread("窗口一",object);
        Thread th2 = getThread("窗口二",object);
        Thread th3 = getThread("窗口三",object);
        Thread th4 = getThread("窗口四",object);
        Thread th5 = getThread("窗口五",object);
        Thread th6 = getThread("窗口六",object);
        th1.start();
        th2.start();
        th3.start();
        th4.start();
        th5.start();
        th6.start();
    }

    public static Thread getThread(String name,Object object) {
        Thread th= new Thread(() -> {
            while (true) {
                synchronized (object) {
                    if (count > 0) {
                        count--;
                        System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
                    } else {
                        break;
                    }
                }
            }
        });
        th.setName(name);
        return th;
    }
}
